﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wpf树菜单项拖拽功能Demo2
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl1 : UserControl
    {  
        TreeItem _treeItem;
        TreeItem treeItem
        {
            get
            {
                if (_treeItem == null)
                    _treeItem = DataContext as TreeItem;
                return _treeItem;
            }
        }
        public UserControl1()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 鼠标按下事件1
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            try
            {
                Console.WriteLine("鼠标按下事件");
                base.OnMouseDown(e);

                var obj = e.OriginalSource as FrameworkElement;
                var data = obj.DataContext as TreeItem;

                DataObject dragData = new DataObject();
                dragData.SetData("TreeItemModel", data);
                dragData.SetData("Object", this);

                DragDrop.DoDragDrop(this, dragData, DragDropEffects.Copy | DragDropEffects.Move);
            }catch(Exception ex)
            {

            }
        }
        /// <summary>
        /// 拖拽移动时2
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDragOver(DragEventArgs e)
        {
            //Console.WriteLine("拖拽移动时");
            var isAdd = e.OriginalSource is Rectangle || e.OriginalSource is Grid;

            if (isAdd == true)
                e.Effects = DragDropEffects.Move;
            else
                e.Effects = DragDropEffects.Copy;

            var data = e.Data.GetData("TreeItemModel") as TreeItem;
            var tagElement = e.OriginalSource as FrameworkElement;
            var tagData = tagElement.DataContext as TreeItem;

            if (IsAddChi(data, tagData))
                e.Effects = DragDropEffects.None;

            e.Handled = true;
        }
        /// <summary>
        /// 拖拽离开控件时3
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDragLeave(DragEventArgs e)
        {
            Console.WriteLine("拖拽离开控件时");
            base.OnDragLeave(e);
            treeItem.Add = false;

            var data = e.Data.GetData("TreeItemModel") as TreeItem;
            var tagElement = e.OriginalSource as FrameworkElement;
            var tagData = tagElement.DataContext as TreeItem;

            e.Effects = DragDropEffects.Move;
            if (IsAddChi(data, tagData))
                e.Effects = DragDropEffects.None;

            e.Handled = true;
        }
        /// <summary>
        /// 拖拽进入控件时4
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDragEnter(DragEventArgs e)
        {
            Console.WriteLine("拖拽进入控件时");
            base.OnDragEnter(e);

            var data = e.Data.GetData("TreeItemModel") as TreeItem;
            var tagElement = e.OriginalSource as FrameworkElement;
            var tagData = tagElement.DataContext as TreeItem;

            var isAdd = e.OriginalSource is Rectangle || e.OriginalSource is Grid;
            if (isAdd && IsAddChi(data, tagData) == false)
                treeItem.Add = true;

            e.Effects = DragDropEffects.Move;

            if (IsAddChi(data, tagData))
                e.Effects = DragDropEffects.None;

            e.Handled = true;
        }
        /// <summary>
        /// 拖拽控件释放时5
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDrop(DragEventArgs e)
        {
            try
            {
                Console.WriteLine("拖拽控件释放时");
                var data = e.Data.GetData("TreeItemModel") as TreeItem;
                var tagElement = e.OriginalSource as FrameworkElement;
                var tagData = tagElement.DataContext as TreeItem;

                if (IsAddChi(data, tagData))
                    return;

                var isAddIn = false;
                if (e.OriginalSource is TextBlock)
                    isAddIn = true;

                var oldParChi = data.Parent?.Childern ?? data.Root;
                if (oldParChi == null)
                    throw new Exception("旧子树空异常");

                oldParChi.Remove(data);

                //判断为兄弟
                if (isAddIn == false)
                {
                    var list = tagData.Parent?.Childern ?? tagData.Root;
                    var borIndex = list.IndexOf(tagData) + 1;

                    data.Parent = tagData.Parent;
                    list.Insert(borIndex, data);
                }

                //新节点
                if (isAddIn == true)
                {
                    data.Parent = tagData;
                    tagData.Childern.Add(data);
                }

                if (data.Parent == null)
                    data.Root = tagData.Root;

                treeItem.Add = false;
            }catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
   

        bool IsAddChi(TreeItem old, TreeItem @new)
        {
            while (@new.Parent != null)
            {
                if (old.TextName == @new.TextName)
                    return true;

                @new = @new.Parent;
            }

            return false;
        }
    }
}
